package com.skytech.tax.customsdatawarehouse.singlewindow.customsdeclarations.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.customsdeclarations.domain.entity.IECustomsDeclaration;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lianhong
 * @since 2021/6/10
 */
@Repository("singleWindow.ieCustomsDeclarationDao")
public class IECustomsDeclarationDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String selectCustomsDeclarationsSql = "SELECT cusCiqNo, customsDeclaration, storageTime, updateTime, ciqIEFlag, entryId, socialCreditCode FROM gd_jckhwgd WHERE 1=1";

    public List<IECustomsDeclaration> findAll() {
        return namedParameterJdbcTemplate.getJdbcOperations()
                .query(this.selectCustomsDeclarationsSql, new BeanPropertyRowMapper<>(IECustomsDeclaration.class));
    }

    public List<IECustomsDeclaration> findByCusCiqNos(List<String> cusCiqNos) {
        Map<String, Object> params = new HashMap<>();
        params.put("cusCiqNos", cusCiqNos);
        return namedParameterJdbcTemplate.query(this.selectCustomsDeclarationsSql + " AND cusCiqNo in (:cusCiqNos)", params, new BeanPropertyRowMapper<>(IECustomsDeclaration.class));
    }

    private String saveCustomsDeclarationsSql = "INSERT INTO gd_jckhwgd (cusCiqNo, customsDeclaration, storageTime, updateTime, ciqIEFlag, entryId, socialCreditCode) " +
            "VALUES (:cusCiqNo, :customsDeclaration, :storageTime, :updateTime, :ciqIEFlag, :entryId, :socialCreditCode)";

    public void save(IECustomsDeclaration customsDeclaration) {
        customsDeclaration.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.saveCustomsDeclarationsSql, new BeanPropertySqlParameterSource(customsDeclaration));
    }

    private String deleteCustomsDeclarationSql = "DELETE FROM gd_jckhwgd WHERE cusCiqNo = ?";

    public void delete(String cusCiqNo) {
        this.namedParameterJdbcTemplate.getJdbcOperations().update(this.deleteCustomsDeclarationSql, cusCiqNo);
    }

    public IECustomsDeclaration findByCusCiqNo(String cusCiqNo) {
        StringBuilder sql = new StringBuilder(this.selectCustomsDeclarationsSql);
        List<Object> params = new ArrayList<>();
        if (StringUtils.isNotEmpty(cusCiqNo)) {
            sql.append(" AND cusCiqNo = ? ");
            params.add(cusCiqNo);
        }
        try {
            return namedParameterJdbcTemplate.getJdbcOperations()
                    .queryForObject(sql.toString(), params.toArray(), new BeanPropertyRowMapper<>(IECustomsDeclaration.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
